package com.ruoyi.lc.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lc.domain.LcDatamodelColumn;
import com.ruoyi.lc.domain.LcDatamodelDefine;
import com.ruoyi.lc.enums.LcDatamodelDefineStateEnum;
import com.ruoyi.lc.mapper.LcDatamodelDefineMapper;
import com.ruoyi.lc.mapper.TableMapper;
import com.ruoyi.lc.service.ILcDatamodelDefineService;
import com.ruoyi.lc.util.LcDataModelUtil;

/**
 * 数据建模Service业务层处理
 * 
 * @author qibotian
 * @date 2025-01-07
 */
@Service
public class LcDatamodelDefineServiceImpl implements ILcDatamodelDefineService 
{
    @Autowired
    private LcDatamodelDefineMapper lcDatamodelDefineMapper;
    
    @Resource
    private TableMapper tableMapper;

    /**
     * 查询数据建模
     * @param id 数据建模主键
     * @return 数据建模
     */
    @Override
    public LcDatamodelDefine selectLcDatamodelDefineById(Long id)
    {
        return lcDatamodelDefineMapper.selectLcDatamodelDefineById(id);
    }

    /**
     * 查询数据建模列表
     * 
     * @param lcDatamodelDefine 数据建模
     * @return 数据建模
     */
    @Override
    public List<LcDatamodelDefine> selectLcDatamodelDefineList(LcDatamodelDefine lcDatamodelDefine)
    {
        return lcDatamodelDefineMapper.selectLcDatamodelDefineList(lcDatamodelDefine);
    }

    /**
     * 新增数据建模
     * 
     * @param lcDatamodelDefine 数据建模
     * @return 结果
     */
    @Override
    public int insertLcDatamodelDefine(LcDatamodelDefine lcDatamodelDefine)
    {
        lcDatamodelDefine.setCreateTime(DateUtils.getNowDate());
        lcDatamodelDefine.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        lcDatamodelDefine.setState(LcDatamodelDefineStateEnum.XJ.getCode());
        lcDatamodelDefine.setWhzTitle(lcDatamodelDefine.getTitle());
        lcDatamodelDefine.setWhzDesc(LcDataModelUtil.genTableDesc(lcDatamodelDefine, LcDataModelUtil.genDefaultColumns(lcDatamodelDefine.getCode())));
        return lcDatamodelDefineMapper.insertLcDatamodelDefine(lcDatamodelDefine);
    }
    
    /**
     * 维护数据建模
     * @param lcDatamodelDefine 数据建模
     * @return 结果
     */
    @Override
    public int weihu(Long id)
    {
    	LcDatamodelDefine lcDatamodelDefine = selectLcDatamodelDefineById(id);
        lcDatamodelDefine.setUpdateTime(DateUtils.getNowDate());
        lcDatamodelDefine.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        lcDatamodelDefine.setState(LcDatamodelDefineStateEnum.WHZ.getCode());
        lcDatamodelDefine.setWhzDesc(lcDatamodelDefine.getDesc());
        lcDatamodelDefine.setWhzTitle(lcDatamodelDefine.getTitle());
        return lcDatamodelDefineMapper.updateLcDatamodelDefine(lcDatamodelDefine);
    }
    

    /**
     * 修改数据建模
     * 
     * @param lcDatamodelDefine 数据建模
     * @return 结果
     */
    @Override
    public int updateLcDatamodelDefine(LcDatamodelDefine lcDatamodelDefine)
    {
    	String title = lcDatamodelDefine.getTitle();
    	lcDatamodelDefine = selectLcDatamodelDefineById(lcDatamodelDefine.getId());
        lcDatamodelDefine.setUpdateTime(DateUtils.getNowDate());
        lcDatamodelDefine.setUpdateBy(SecurityUtils.getUsername());
        lcDatamodelDefine.setWhzTitle(title);
        return lcDatamodelDefineMapper.updateLcDatamodelDefine(lcDatamodelDefine);
    }

    /**
     * 删除数据建模信息
     * 
     * @param id 数据建模主键
     * @return 结果
     */
    @Override
    public int deleteLcDatamodelDefineById(Long id)
    {
    	LcDatamodelDefine define = selectLcDatamodelDefineById(id);
    	if(LcDatamodelDefineStateEnum.OK.getCode().equals(define.getState()) || LcDatamodelDefineStateEnum.XJ.getCode().equals(define.getState())) {
    		if(tableMapper.checkTableExists(define.getCode()) > 0) {
    			tableMapper.exec("DROP TABLE IF EXISTS `" + define.getCode() + "`");
    		}
    		 return lcDatamodelDefineMapper.deleteLcDatamodelDefineById(id);
    	} else {
    		define.setState(LcDatamodelDefineStateEnum.OK.getCode());
    		return lcDatamodelDefineMapper.updateLcDatamodelDefine(define);
    	}
    }

	@Override
	public LcDatamodelDefine selectLcDatamodelDefineByCode(String code) {
		return lcDatamodelDefineMapper.selectLcDatamodelDefineByCode(code);
	}

	@Override
	public int saveColumn(LcDatamodelColumn column) {
		LcDatamodelDefine define = selectLcDatamodelDefineByCode(column.getDefineCode());
		List<LcDatamodelColumn> columns = LcDataModelUtil.findColumnsFromDesc(define.getWhzDesc());
		boolean finded = false;
		for(LcDatamodelColumn col : columns) {
			if(col.getCode().equals(column.getCode())) {
				col.setLengthD(column.getLengthD());
				col.setLengthM(column.getLengthM());
				col.setMapping(column.getMapping());
				col.setMappingType(column.getMappingType());
				col.setNullable(column.getNullable());
				col.setOrderNum(column.getOrderNum());
				col.setPkey(column.getPkey());
				col.setRemark(column.getRemark());
				col.setState(column.getState());
				col.setTitle(column.getTitle());
				col.setType(column.getType());
				col.setUpdateBy(SecurityUtils.getUsername());
				col.setUpdateTime(new Date());
				finded = true;
				break;
			}
		}
		if(!finded) {
			column.setCreateBy(SecurityUtils.getUsername());
			column.setCreateTime(new Date());
			column.setOrderNum(columns.size());
			columns.add(column);
		}
		String desc = LcDataModelUtil.genTableDesc(define, columns);
		define.setWhzDesc(desc);
		return lcDatamodelDefineMapper.updateLcDatamodelDefine(define);
	}

	@Override
	public int deleteColumn(String defineCode, String columnCode) {
		LcDatamodelDefine define = selectLcDatamodelDefineByCode(defineCode);
		List<LcDatamodelColumn> columns = LcDataModelUtil.findColumnsFromDesc(define.getWhzDesc());
		for(LcDatamodelColumn col : columns) {
			if(col.getCode().equals(columnCode)) {
				columns.remove(col);
				break;
			}
		}
		String desc = LcDataModelUtil.genTableDesc(define, columns);
		define.setWhzDesc(desc);
		return lcDatamodelDefineMapper.updateLcDatamodelDefine(define);
	}

	@Override
	public int publish(Long id) {
		LcDatamodelDefine define = selectLcDatamodelDefineById(id);
		if(LcDatamodelDefineStateEnum.WHZ.getCode().equals(define.getState())) {
			String manSql = LcDataModelUtil.generateAlterTableSql(define);
			tableMapper.exec(manSql);
		} else if (LcDatamodelDefineStateEnum.XJ.getCode().equals(define.getState())) {
			String manSql = LcDataModelUtil.genCreateSql(define);
			tableMapper.exec(manSql);
		}
		define.setDesc(define.getWhzDesc());
		define.setTitle(define.getWhzTitle());
		define.setUpdateTime(DateUtils.getNowDate());
		define.setUpdateBy(SecurityUtils.getUsername());
		define.setState(LcDatamodelDefineStateEnum.OK.getCode());
		return lcDatamodelDefineMapper.updateLcDatamodelDefine(define);
	}
}
